package app.webchat.payments.agree;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.switfpass.pay.MainApplication;
import com.switfpass.pay.activity.PayPlugin;
import com.switfpass.pay.bean.RequestMsg;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import app.webchat.payments.agree.event.WXPayResultListener;
import app.webchat.payments.agree.utils.Constants;
import app.webchat.payments.agree.utils.WXPayResultManager;

/**
 * 这个类Pay.java中 跳转用到，必须在包名下
 * 
 * 20161116之后的版本不会跳转到这个类，不过不能删除老版本兼容需要用到
 * @author Administrator
 *
 */
public class WXPayActivity extends Activity implements WXPayResultListener {

	private static final String TAG = "WXPayActivity";

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.w(TAG, "onCreate");
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// activity 跳转传的值
		Bundle bundle = getIntent().getExtras();
		if(null != bundle){
			String wxappid = bundle.getString("wxappid", "");
			String wxpaytype = bundle.getString("wxpaytype", "");

			Constants.WXAPPID = wxappid;
			Constants.wxPayType = wxpaytype;
			Constants.goodsType = bundle.getString("goodstype", "");
			Constants.gamePack = bundle.getString("gamepack", "");

			Log.w(TAG, "wxpaytype:" + wxpaytype);
			Log.w(TAG, "gamePack:" + bundle.getString("gamePack", ""));
			if ("1".equals(wxpaytype)){//威富通支付
				String tokenid = bundle.getString("tokenid", "");
				startWFTPay(tokenid, wxappid);
			}else if ("0".equals(wxpaytype)){
				Log.w(TAG, "wxpay");
				WXPayInfo payInfo = new WXPayInfo();
				payInfo.appId = bundle.getString("wxappid", "");
				payInfo.partnerId = bundle.getString("partnerid", "");
				payInfo.prepayId = bundle.getString("prepayid", "");
				payInfo.timeStamp = bundle.getString("timestamp", "");
				payInfo.nonceStr = bundle.getString("noncestr", "");
				payInfo.sign = bundle.getString("sign", "");
				payInfo.wxPackage = bundle.getString("wxpackage", "");

				wxPay(payInfo);
			}
		}else {
			finish();
		}
	}


	/**
	 * 威富通支付
	 * @param tokenid
	 * @param wxappid
	 */
	private void startWFTPay(String tokenid, String wxappid) {
		try {
			Log.w(TAG, "startWFTPay");
			RequestMsg msg = new RequestMsg();
			msg.setTokenId(tokenid);
			msg.setTradeType(MainApplication.WX_APP_TYPE);
			msg.setAppId(wxappid);
			PayPlugin.unifiedAppPay(WXPayActivity.this, msg);
			WXPayResultManager.Instener().addWatcher(this);
		} catch (Exception e) {
			Log.w(TAG, "startWXPay:" + e);
		}
	}

	/**
	 * 微信支付
	 */
	private void wxPay(WXPayInfo payInfo){
		Constants.WXAPPID = payInfo.appId;

		PayReq req = new PayReq();
		req.appId = payInfo.appId;
		req.partnerId = payInfo.partnerId;
		req.prepayId = payInfo.prepayId;
		req.nonceStr = payInfo.nonceStr;
		req.timeStamp = payInfo.timeStamp;
		req.sign = payInfo.sign;
		req.packageValue = payInfo.wxPackage;
//		req.extData = "app data"; // optional

		IWXAPI api = WXAPIFactory.createWXAPI(WXPayActivity.this, Constants.WXAPPID);
		api.registerApp(Constants.WXAPPID);
		boolean flag = api.sendReq(req);
		WXPayResultManager.Instener().addWatcher(this);
	}

	@Override
	public void payFinish() {
		finish();
	}


	@Override
	protected void onDestroy() {
		Log.i(TAG, "onDestroy");
		WXPayResultManager.Instener().removeWatcher(this);
		super.onDestroy();
	}


	class WXPayInfo{
		String appId;
		String partnerId;
		String prepayId;
		String nonceStr;
		String timeStamp;
		String sign;
		String wxPackage;
	}
}
